package app.netant.finishbook.sougoutxt;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class SougouTxtDetail extends BaseDetail {

	public SougouTxtDetail(String netSite) {
		super(netSite);
		this.preHref = "http://www.sogoutxt.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select("body .bbody .w960 .pleft3 .viewbox .title h2");
		if(parts.size()>0){
			String title = parts.get(0).text();
			int st = title.indexOf("《");
			int ed = title.indexOf("》");
			if(st>=0 && ed>=0 && ed>st){
				title = title.substring(st+1,ed);
			}
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".rar");
		}
	}

	@Override
	public void analyseTags() {
		Elements parts1 = this.select("body .bbody .w960 .pleft3 .viewbox .infolist small");
		Elements parts2 = this.select("body .bbody .w960 .pleft3 .viewbox .infolist span");
		for(int i=0;i<parts1.size();i++){
			if(parts2.size()>i){
				String tagName = parts1.get(i).text().trim();
				String tagValue =parts2.get(i).text().trim();
				
				if(tagName.endsWith("：")){
					tagName = tagName.substring(0,tagName.length()-1);
				}
				
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select("body .bbody .w960 .pleft3 .viewbox .content div");
		if(parts.size()>0){
			String memo = this.getString(parts.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		
		Elements parts = this.select("body .bbody .w960 .pleft3 .viewbox .content ul.downurllistad .downurllist a");
		if(parts.size()>0){
			String url = parts.get(0).attr("href");
			if(url.startsWith("/")){
				url = this.preHref + url;
			}
			System.out.println("【Url】:"+url);
			
			SougouTxtDetail detail = new SougouTxtDetail(url);
			String href = detail.stepHref();
			
			System.out.println("【Href】:"+href);
			this.entity.setDownPath(href);
		}
		
	}
	
	public String stepHref(){
		Elements parts = this.select("body div[align='center'] center ul li a");
		if(parts.size()>0){
			String url = parts.get(0).attr("href");
			if(url.startsWith("/")){
				url = this.preHref + url;
			}
			//System.out.println("【Href】:"+url);			
			return url;
		}	
		return null;
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select("body .bbody .w960 .pleft3 .place a");
		String type = "";
		if(parts.size()>2){
			type = parts.get(2).text().trim();
		}else if(parts.size()>1){
			type = parts.get(1).text().trim();
		}
		System.out.println("【Type】:"+type);
		this.entity.setNovelType(type);
	}
	
	public static void main(String[] args) {
		SougouTxtDetail detail = new SougouTxtDetail("http://www.sogoutxt.com/qihuan/yijiedalu/10780.html");
		detail.test();
	}



}
